package com.itheima.stock.service.impl;

import com.itheima.stock.cache.impl.RedisCache;
import com.itheima.stock.constant.Constants;
import com.itheima.stock.dto.LoginDTO;
import com.itheima.stock.entity.SysUser;
import com.itheima.stock.enums.CodeEnum;
import com.itheima.stock.exception.Exceptions;

import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.service.UserService;
import com.itheima.stock.util.IdWorkers;
import com.itheima.stock.util.RedisKeyUtil;
import com.itheima.stock.vo.CaptchaVO;
import com.itheima.stock.vo.LoginVO;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.sql.Time;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    private static final int FOUR = 4;

    private final RedisCache redisCache;

    private final IdWorkers idWorkers;

    private final SysUserMapper sysUserMapper;

    private final PasswordEncoder passwordEncoder;
    public UserServiceImpl(RedisCache redisCache, IdWorkers idWorkers, SysUserMapper sysUserMapper, PasswordEncoder passwordEncoder) {
        this.redisCache = redisCache;
        this.idWorkers = idWorkers;
        this.sysUserMapper = sysUserMapper;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public LoginVO login(LoginDTO loginDTO) {
        //1.参数校验
        if(ObjectUtils.isEmpty(loginDTO)
                || ObjectUtils.isEmpty(loginDTO.getUsername())
                || ObjectUtils.isEmpty(loginDTO.getPassword())
                || ObjectUtils.isEmpty(loginDTO.getRkey())
                || ObjectUtils.isEmpty(loginDTO.getCode())
        ) {
            Exceptions.cast(CodeEnum.DATA_ERROR);
        }
        //2.核心业务逻辑
        //2.1判断验证码是否正确
        String captchaRedis = redisCache.get(loginDTO.getRkey());
        if(!StringUtils.equals(loginDTO.getCode(), captchaRedis)) {
            Exceptions.cast(CodeEnum.SYSTEM_VERIFY_CODE_ERROR);
        }
        redisCache.rem(loginDTO.getRkey());
        //2.2判断用户是否为空，密码是否正确
        SysUser user = sysUserMapper.selectByUsername(loginDTO.getUsername(),Constants.SYS_USER_STATUS_NORMAL,Constants.SYS_USER_UN_DELETED);
        if(ObjectUtils.isEmpty(user)||!passwordEncoder.matches(loginDTO.getPassword(), user.getPassword())) {
            Exceptions.cast(CodeEnum.SYSTEM_PASSWORD_ERROR);
        }
        //3.封装数据返回
        return LoginVO.builder()
                .id(user.getId())
                .username(loginDTO.getUsername())
                .phone(user.getPhone())
                .nickName(user.getNickName())
                .build();
    }

    @Override
    public CaptchaVO captcha() {
        //1.生成验证码
        String captcha = RandomStringUtils.randomNumeric(FOUR);
        //2.将验证码存入redis
        String redisKey = RedisKeyUtil.generateKey(Constants.CAPTCHA_PREFIX, idWorkers.nextStrId());
        redisCache.set(redisKey, captcha, Constants.CAPTCHA_TIMEOUT_TIME);
        //3.封装数据返回
        return CaptchaVO.builder()
                .code(captcha)
                .rkey(redisKey)
                .build();
    }
}
